home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Loadstar 1
/
001.d81
/
cows and bulls
(
.txt
)
< prev
next >
Wrap
Commodore BASIC
|
2022-08-26
|
7KB
|
256 lines
5 rem *** cows and bulls ***
10 rem
100 rem---- documentation module ------
110 rem
120 rem r$ = general purpose variable
130 rem np = number of players
140 rem pn$(10)close= players names
150 rem sn (4) = four digits for 155 rem secret number
160 rem tn = turn number
170 rem gs$ = string for guesses
180 rem gsclose(4) = four digit guess
190 rem bu = bulls
200 rem co = cows
210 rem ag$ = another game switch
220 rem wn$ = win switch
230 rem i = general index
240 rem j = general index
250 rem
300 rem---- housekeeping module -------
310 rem
320 dim pn$ (10)
330 dim sn (04)
340 dim gs (04)
345 sp$=" "
350 rem
500 rem---- executive module ----------
510 rem
515 gosub 9000
520 gosub 1000
521 rem ... game instructions ...
530 gosub 2000
531 rem ... get player's names...
540 gosub 3000
541 rem ... play a game ...
550 if ag$<>"y" then 560
555 print"[147] new game[146]":for i = 1 to 3000: next i
557 goto 540
558 poke53281,0:print"[144][147]load"chr$(34);"hello";chr$(34)",8":print"run"
559 poke 631,13:poke632,13:poke198,2:poke65535,ix(k,1)
560 goto 63000
1000 rem---- game instructions --------
1010 rem
1020 poke 53281,7:poke 53280,6:print"[144][147]":poke657,128
1025 printchr$(14)
1030 print" ";
1040 print"[195]ows & [194]ulls"
1050 print"";
1060 input "[196]o you need instructions?[160](y or n) "; r$
1070 if r$= "n" then 1990
1080 print"[147]"
1090 print"[195]ows & [194]ulls is designed to stimulate"
1100 print"your deductive powers."
1110 print:print"[212]he object of [195]ows & [194]ulls is to guess"
1120 print"a secret number known only to your":print"[195]ommodore."
1125 print:print"[217]our [195]ommodore generates the secret"
1126 print"number at random according to the"
1127 print"following restrictions:"
1130 print:print" 1. [212]he number has four digits"
1131 print:print" 2. [206]o digit may be repeated"
1135 print:print"[214]alid examples are:"
1136 print:print" 1234":print" 0526 (may start with zero)"
1137 print" 9870"
1140 gosub9000:print:print:input" ** hit return **";r$
1145 print"[147]":print"[208]lay goes like this---"
1146 print:print" 1. [217]ou make a guess as to what you"
1147 print" think the secret number is."
1148 print:print" 2. [217]our [195]ommodore evaluates your guess";
1149 print" and responds with an indication"
1150 print" of how good your guess was."
1151 print:print" 3. [217]ou record your guess and the"
1152 print" response to it."
1153 print:print" 4. [212]hen you formulate another guess"
1154 print" based on previous guesses and"
1155 print" responses."
1156 print:print" 5. [212]he first person to guess the"
1157 print" [195]ommodore's secret number wins.":print:print
1160 gosub9000:input" ** hit return **";r$
1165 print"[147]":print"[212]his is show the commodore evalutes"
1166 print"your guess---":print:print" 1. [198]or each digit that you have in the"
1167 print" correct position you are awarded":print" one 'bull'."
1170 print:print" 2. [198]or a digit that is in the secret"
1171 print" number but not in thte correct"
1172 print" position you are awarded one 'cow'."
1175 print:print" 3. [196]igits that do not appear in the"
1176 print" number at all generate no response.":gosub9000
1180 print" ";:input"** hit return **";r$
1185 print"[147]":print"[193]ssume the secret number is:"
1186 printtab(13)"7032":print:print"then these are examples of guesses and"
1187 print"responses.":print
1190 printtab(13)"1243 2 cows"
1191 printtab(13)"2743 4 cows"
1192 printtab(13)"8039 2 bulls"
1193 printtab(13)"8079 1 bull & 1 cow"
1194 printtab(13)"7032 4 bulls (win)":gosub9000
1195 print"":input" ** hit return **";r$
1200 print"[147]":print" --- [200][201][206][212] ---"
1201 print:print"[205]ake your first two guesses"
1202 print:print" 1234 and 5678":print
1205 print"[212]hen if you have four responses from"
1206 print"your first two guesses,"
1207 print" you will know 9 & 0 are not in"
1208 print" the secret number."
1210 print:print"[201]f you have two responses from your"
1211 print"first two guesses,"
1212 print" you will know 9 & 0 are in the"
1213 print" secret number."
1215 print:print"[215]hat do you know if you have three"
1216 print"responses from the first two guesses?":print
1220 gosub9000:print" ... [199][207][207][196][160][204][213][195][203] ..."
1221 print" ** hit return **";
1222 input"";r$
1990 return
2000 rem---- get player's names -------
2010 np = 0
2020 print"[147]"
2030 np = np + 1
2040 print"[197]nter the name of player #[160]";np
2045 gosub9000
2050 print:input"[160]";pn$(np)
2060 print"[147]"
2070 input"[193]ny more players? (y or n) ";r$
2080 if r$= "y" then 2020
2090 return
3000 rem---- play a game --------------
3010 rem
3015 gosub9000
3020 gosub 4000
3021 rem ... generate secret number...
3030 gosub 5000
3031 rem ... present a turn
3040 if wn$ = "n" then 3030
3041 rem no win???
3050 if tn <>np then 3030
3060 return
4000 rem---- generate secret number ---
4010 rem
4020 i = 0
4030 i = i + 1
4040 if i > 4 then 4110
4050 sn (i) = int (rnd (1)* 10)
4060 j = 0
4070 j = j + 1
4080 if j = i then 4030
4090 if sn (i) = sn (j) then 4050
4100 goto 4070
4110 wn$ = "n"
4111 rem wn$close= win switch
4120 return
5000 rem---- present a turn -----------
5010 rem
5020 tn = tn + 1
5030 if tn > np then tn = 1
5040 print"[147]"
5050 print"[201]t is " pn$(tn) "'s turn"
5060 print""
5070 print"[207]ther players don't look"
5080 print""
5090 print"[208]ress a key "pn$(tn)"."
5100 getk$:if k$=""then 5100
5105 gosub9000
5110 gosub 6000
5111 rem ... get a guess ...
5120 gosub 7000
5121 rem ... evaluate guess ...
5130 gosub 8000
5131 rem ... display guess result...
5140 return
6000 rem---- get a guess --------------
6010 rem
6020 print"[147]"
6030 printtab(10)"[207][203], "pn$(tn)
6040 print"":print"[197]nter your guess, 4 digits, no repeats"
6050 print" ----"
6055 print" ";
6059 gs$=""
6060 input"";gs$
6070 if len (gs$) <> 4 then 6180
6072 if left$(gs$,1)="e" then 6180
6074 if left$(gs$,1)="+"then 6180
6080 if val (gs$)<123 then 6180
6081 if val (gs$)> 9876 then 6180
6090 i = 0
6100 i = i + 1
6110 if i > 4 then 6300
6120 gs (i) = val (mid$(gs$,i,1))
6130 j = 0
6140 j = j + 1
6150 if j = i then 6100
6160 if gs (i) = gs (j) then 6180
6170 goto 6140
6180 print""
6190 print" "
6200 print"[145][145] ";
6210 gosub10000:print"try again[146]"
6240 for i = 1 to6:gosub9000:nexti:gosub11000
6250 print"":printsp$:printsp$:printsp$
6260 goto 6050
6300 return
7000 rem---- evaluate guess -----------
7010 rem
7020 bu = 0: co = 0
7030 for i = 1 to 4
7040 for j = 1 to 4
7050 if i = j then if gs (j) = sn (i) then bu = bu + 1
7060 if i <> j then if gs (j)= sn (i) then co = co + 1
7070 next j
7075 gosub 9000
7080 next i
7090 return
8000 rem---- display guess resultclose-----
8010 rem
8020 print"[147]";tab(18);pn$(tn)
8025 print""spc(15);
8027 print"guess = ";gs$
8030 print""spc(17);
8040 printco;" cow";:if co<>1 then print"s";
8045 print
8050 print""spc(17);
8060 printbu" bull";:if bu<>1 then print"s";
8065 print
8068 if bu = 4 then 8200
8069 if wn$= "y" then 8255
8070 print" ";"record your guess and result"
8080 print" ** then hit return **":input"";r$
8090 goto 8500
8200 print""spc(16);
8205 wn$= "y"
8210 print"[212]hat's it[146]"
8220 if tn = np then 8270
8230 print" [204]et the others finish their turn."
8240 print" ";:input"** hit return **";r$
8250 goto 8500
8255 if tn <> np then 8070
8260 print" [212]here was a winner"
8270 print"